package com.bocai.dahuatech.utils;

import android.app.DownloadManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;

public class DownLoadApkUtil {
    Context context;
    String url;
    String version;
    public DownLoadApkUtil(Context context,String url,String version){
        this.context=context;
        this.url=url;
        this.version=version;
    }


//    @SuppressLint("NewApi")

//    @TargetApi(Build.VERSION_CODES.GINGERBREAD)

    public void intoDownloadManager(){

        DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

        Uri uri = Uri.parse(url);

        DownloadManager.Request request = new DownloadManager.Request(uri);

        // 设置下载路径和文件名

        request.setDestinationInExternalPublicDir("download", "dahua.apk");

        request.setDescription("大华分销新版本下载");

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        request.setMimeType("application/vnd.android.package-archive");

        // 设置为可被媒体扫描器找到

        request.allowScanningByMediaScanner();

        // 设置为可见和可管理

        request.setVisibleInDownloadsUi(true);

        long refernece = dManager.enqueue(request);

        // 把当前下载的ID保存起来

        SharedPreferences sPreferences = context.getSharedPreferences("downloadplato",0);

        sPreferences.edit().putLong("plato", refernece).commit();

    }

}
